#    Supsta
#    Copyright (c) 2014 Sebastian Dufner
#
#    This file is part of Supsta.
#
#    Supsta is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Supsta is distributed in the hope that it will be useful,#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

##

module Supsta

  ##
  # Raised when a requested subclass of FilterRenderer could not be found.
  # This most likely means that a Supsta project was saved using a renderer
  # that is not supported by the current distribution.

  class NoRendererError < NameError
    def initialize(renderer, *args)
      super("LayerRenderer not found: #{renderer}", *args)
    end
  end

  ##
  # Provides the rendering magic for the Filter class. Subclasses must override
  # the method #run. If the renderer requires some special setup parameters,
  # the #params method should be overriden also.

  class FilterRenderer

    # Returns a FilterRenderer, restoring it from +hash+.

    def self.unhash(hash)
      cls = case hash[:class]
      when Class            then hash[:class]
      when String, Symbol   then Module.const_get(hash[:class])
      else fail NoRendererError, hash[:class]
      end

      cls.new(*hash[:params])
    end

    def initialize(*args)
      # do nothing
    end

    ##
    # Returns an Array of the exact parameters that should be passed to #new to
    # create an exact copy of this FilterRenderer.

    def params
      []
    end

    ##
    # Executes whatever operations this renderer supports on the given Image
    # and Layer instances.

    def run(image, layer)
      fail NotImplementedError, 'all subclasses of FilterRenderer must implement this method'
    end

    ##
    # Returns a Hash representing this FilterRenderer.

    def to_hash
      {class: self.class.to_s, params: params}
    end

  end
end



